/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Bruno
 */
class ThreadCampo extends Thread {

    public void run() {
        for (;;) {
            if (PedeSenha.campoSenha.getText().length() > 15) {
                PedeSenha.campoSenha.setText(PedeSenha.campoSenha.getText().substring(0, 15));
            }
            try {
                sleep(300);
            } catch (InterruptedException ex) {
                Logger.getLogger(ThreadCampo.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

public class PedeSenha extends javax.swing.JDialog {

    ThreadCampo max15a = new ThreadCampo();
    int ondeVai;   //ajuda a saber qual janela abrir depois da confirmacao da senha
    //lista de janelas
    //1 - deve abrir o cadastro de precos para horistas

    /**
     * Creates new form PedeSenha
     */
    public PedeSenha(int i) {
        ondeVai = i;
        initComponents();
        max15a.start();
        setModal(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        textoDigiteASenha = new javax.swing.JLabel();
        campoSenha = new javax.swing.JTextField();
        botaoCancelar = new javax.swing.JButton();
        botaoConfirma = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("PowerPark");

        textoDigiteASenha.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
        textoDigiteASenha.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        textoDigiteASenha.setText("Digite a Senha:");

        campoSenha.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N

        botaoCancelar.setText("Cancelar");
        botaoCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoCancelarActionPerformed(evt);
            }
        });

        botaoConfirma.setText("Confirma");
        botaoConfirma.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoConfirmaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(campoSenha)
                            .addComponent(textoDigiteASenha, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(botaoCancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                        .addComponent(botaoConfirma)))
                .addGap(0, 9, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(textoDigiteASenha)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(campoSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botaoCancelar)
                    .addComponent(botaoConfirma))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-219)/2, (screenSize.height-140)/2, 219, 140);
    }// </editor-fold>//GEN-END:initComponents

    private void botaoCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoCancelarActionPerformed
        max15a.stop();
        dispose();
    }//GEN-LAST:event_botaoCancelarActionPerformed

    private void botaoConfirmaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoConfirmaActionPerformed
        if (campoSenha.getText().length() > 15) {
        } else {
            File arquivo = new File("x01.sp");
            if (arquivo.exists()) { //Verifica se o arquivo de senha já existe. Caso exista...
                String senhaArquivo = null;
                try { //Cria um fluxo para ler do arquivo as atividades ja cadastradas
                    ObjectInputStream leitorArquivo = new ObjectInputStream(new FileInputStream("x01.sp"));
                    senhaArquivo = (String) leitorArquivo.readObject(); //Salva o conteudo do arquvo no ArrayList de atividades
                    leitorArquivo.close();
                } catch (IOException e) {
                } catch (ClassNotFoundException ex) {
                }
                if (senhaArquivo.equals(campoSenha.getText())){
                    if (ondeVai == 1) {
                        CadastroValores cad = new CadastroValores();
                        cad.repaint();
                        cad.setVisible(true);
                        dispose();
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "A senha digitada está incorreta.");
                    dispose();
                }
            } else { //Caso o arquivo de senha não exista ele cria o arquivo
                JOptionPane.showMessageDialog(null, "O arquivo sumiu inesperadamente! o:");
                dispose();
            }
        }
    }//GEN-LAST:event_botaoConfirmaActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoCancelar;
    private javax.swing.JButton botaoConfirma;
    static javax.swing.JTextField campoSenha;
    private javax.swing.JLabel textoDigiteASenha;
    // End of variables declaration//GEN-END:variables
}